package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootSociatyInfo;
import org.footballmanager.pandafootball.system.domain.enums.ZDZW;
import org.footballmanager.pandafootball.system.form.impl.FootSociatyInfoSearchForm;
import org.footballmanager.pandafootball.system.service.FootSociatyInfoService;
import org.footballmanager.pandafootball.system.service.FootSociatyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footSociatyInfoSearchForm")
@RequestMapping("/hy/footSociatyInfo")
public class HySociatyInfoController extends BaseController{
    /* 实体名称 */
    private static final String ENTITYNAME = "footSociatyInfo";
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootSociatyService footSociatyService;
    @Autowired
    private FootSociatyInfoService footSociatyInfoService;

    /**
     * 初始化
     * @param map 参数
     * @param footSociatyInfoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootSociatyInfoSearchForm footSociatyInfoSearchForm){
        FootSociatyInfoSearchForm newFootSociatyInfoSearchForm = new FootSociatyInfoSearchForm();
        if(footSociatyInfoSearchForm != null){
            newFootSociatyInfoSearchForm = footSociatyInfoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootSociatyInfoSearchForm);
    }

    /**
     * 战队管理列表
     * @param map 参数
     * @param footSociatyInfoSearchForm 条件
     * @return 页面
     */
	@RequestMapping(value="/search", method= RequestMethod.GET)
	public String search(Model map, HttpServletRequest request,
                         @ModelAttribute FootSociatyInfoSearchForm footSociatyInfoSearchForm) {
        FootSociatyInfo info = (FootSociatyInfo)request.getSession().getAttribute("mySociaty");
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footSociatyInfoSearchForm);
        map.addAttribute("footSociaty", footSociatyService.get(info.getZdId()));
        map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), footSociatyInfoService.findSociatyBySociatyIdAndAgree(info.getZdId(), true));
        return LISTPAGE;
	}

    /**
     * 加入战队页面
     * @param zdId 战队主键
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/join", method = RequestMethod.GET)
    public String join(@RequestParam Long zdId, HttpServletRequest request) {
        FootSociatyInfo info = (FootSociatyInfo)request.getSession().getAttribute("mySociaty");
        info.setZdId(zdId);
        footSociatyInfoService.save(info, request);
        request.getSession().setAttribute("mySociaty", info);
        return "redirect:/hy/footSociaty/search";
    }

    @RequestMapping(value = "changeRelation", method = RequestMethod.POST)
    @ResponseBody
    public String changeRelation(Long id, ZDZW relation, HttpServletRequest request){
        FootSociatyInfo info = footSociatyInfoService.get(id);
        info.setRelation(relation);
        footSociatyInfoService.updateNotNull(info, request);
        return Constants.OK;
    }

    /**
     * 查看新进队员页面
     * @param map 参数
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/personList", method = RequestMethod.GET)
    public String viewNewPerson(Model map, HttpServletRequest request,
                                @ModelAttribute FootSociatyInfoSearchForm footSociatyInfoSearchForm) {
        FootSociatyInfo info = (FootSociatyInfo)request.getSession().getAttribute("mySociaty");
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footSociatyInfoSearchForm);
        map.addAttribute("footSociaty", footSociatyService.get(info.getZdId()));
        map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), footSociatyInfoService.findSociatyBySociatyIdAndAgree(info.getZdId(), false));
        return "hy/footSociatyInfo/PersonList";
    }

    @RequestMapping(value = "agree", method = RequestMethod.POST)
    @ResponseBody
    public String agree(@RequestParam  Long id, @RequestParam Boolean agree, HttpServletRequest request){
        footSociatyInfoService.agree(id, agree, request);
        return Constants.OK;
    }

    /**
     * 退出战队页面
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/exit", method = RequestMethod.GET)
    public String exit(HttpServletRequest request, @RequestParam Long userId) {
        footSociatyInfoService.exitSociaty(userId, request);
        return "redirect:/login";
    }
}
